﻿/*******************************************************************************************
REPEATER CONTROL
*******************************************************************************************/

JUI.Repeater = function (params) {

    this.tagName = "JUI.Repeater";

    if (!params) params = {}

    /** ATTRIBUTES : HTML **/
    if (params.containerElement) this.containerElement = params.containerElement;
    else this.containerElement = null;

    if (params.itemTemplate) this.itemTemplate = params.itemTemplate;
    else this.itemTemplate = "I am the default repeater template";

    if (params.editTemplate) this.editTemplate = params.editTemplate;
    else this.editTemplate = null;

    if (params.alternateItemTemplate) this.alternateItemTemplate = params.alternateItemTemplate;
    else this.alternateItemTemplate = this.itemTemplate;

    if (params.itemContainer) this.itemContainer = params.itemContainer;                //  div     | li    | td    | span
    else this.itemContainer = "DIV";


    /** ATTRIBUTES : DATA **/
    if (params.dataProvider) this.dataProvider = params.dataProvider;                   //  user    | wsdl  | wcf   | excel
    else this.dataProvider = "user";

    if (params.dataSource) this.dataSource = params.dataSource;
    else this.dataSource = null;

    if (params.dataType) this.dataType = params.dataType;                               //  json    | xml   | csv   | openxml
    else this.dataType = undefined;

    if (params.dataNode) this.dataNode = params.dataNode;
    else this.dataNode = null;


    /** ATTRIBUTES : STYLE **/
    if (params.skinName) this.skinName = params.skinName;
    else this.skinName = "default";

    if (params.onSelectClass) this.onSelectClass = params.onSelectClass;
    else this.onSelectClass = "default";

    if (params.customStyleRules) this.customStyleRules = params.customStyleRules;
    else this.customStyleRules = null;


    /** ATTRIBUTES : EVENTS **/
    if (params.onItemSelecting) this.onItemSelecting = params.onItemSelecting;
    else this.onItemSelecting = null;

    if (params.onItemSelected) this.onItemSelected = params.onItemSelected;
    else this.onItemSelected = null;

    if (params.onItemEdited) this.onItemEdited = params.onItemEdited;
    else this.onItemEdited = null;


    /** ATTRIBUTES : BEHAVIOR **/
    if (params.editable) this.editable = params.editable;
    else this.editable = false;

    if (params.reArrangable) this.reArrangable = params.reArrangable;
    else this.reArrangable = true;

    if (params.selectable) this.selectable = params.selectable;
    else this.selectable = true;


    /** ATTRIBUTES : EXPORT **/
    if (params.exportFormat) this.exportFormat = params.exportFormat;                   //  pdf     | excel | xml
    else this.exportFormat = "cvs";

    // Get the environment information from JUI, as
    // platform : android | tv | pc | handheld
    // renedering engine type : webkit | gecko | IE

    // Call the create method
    this.init(params);
    this.create();
}
JUI.Repeater.Utils = {

    regexp: /{.*?}/ig
}
JUI.Repeater.prototype.init = function (params) {

    if (!this.dataAdapter)
        this.dataAdapter = JUI.AdapterFactory.getSuitableAdapter(params);

    /** Inserts HTML leaf node container **/
    if (this.itemContainer == "td") {

        var table = document.createElement("TABLE");
        table.setAttribute("style", "table-layout: fixed; width: 100%; height: 100%;");
        table.setAttribute("cellpadding", "0");
        table.setAttribute("cellspacing", "0");

        this.containerElement.appendChild(table);
    }
    else if (this.itemContainer == "li") {

        var ul = document.createElement("UL");
        ul.setAttribute("style", "list-style: none; padding: 0px; margin: 0px;");

        this.containerElement.appendChild(ul);
    }

    /** Sets the event listeners **/
    this.touchListener = new JUI.OnTouchListener();
    this.focusListener = new JUI.OnFocusListener();

    if (this.editable && this.editTemplate) {

        this.touchListener.onClick = function (item, data) {

            if (item.isInEdit) return;

            item.innerHTML = this.fillPattern(this.editTemplate, parseInt(item.getAttribute("__didx")));
            item.isInEdit = true;
        }

        this.focusListener.onBlur = function (item, data) {

            if (item.isInEdit && isMousePointInElementRect(event.clientX, event.clientY, item))
                return;

            item.isInEdit = false;
            var html = parseInt(item.getAttribute("__didx")) % 2 == 0 ? this.itemTemplate : this.alternateItemTemplate;
            item.innerHTML = this.fillPattern(html, parseInt(item.getAttribute("__didx")));
        }
    }
}
JUI.Repeater.prototype.create = function () {

    for (var i = 0; i < this.dataAdapter.length(); i++) {

        var leafContainer = document.createElement(this.itemContainer);
        var itemHTML = i % 2 == 0 ? this.itemTemplate : this.alternateItemTemplate;

        leafContainer.innerHTML = this.fillPattern(itemHTML, i);
        leafContainer.setAttribute("__didx", i);
        this.addItem(leafContainer);
    }
}
JUI.Repeater.prototype.addItem = function (item) {

    var leafContainer = null;
    if (this.itemContainer == "li")
        leafContainer = this.containerElement.childNodes[0];
    else if (this.itemContainer == "td") {

        var tr = document.createElement("TR");
        this.containerElement.childNodes[0].appendChild(tr);
        leafContainer = tr;
    }
    else
        leafContainer = this.containerElement;

    leafContainer.appendChild(item);
    item.setAttribute("tabindex", 1);

    item.context = this;
    item.onclick = function () {
        this.context.touchListener.onClick.call(this.context, item, this.context.dataAdapter.getItemAt(parseInt(this.getAttribute("__didx")))); };
    item.onmousedown = function () {
        this.context.touchListener.onMouseDown.call(this.context, item, this.context.dataAdapter.getItemAt(parseInt(this.getAttribute("__didx"))));
    }
    item.onmouseup = function () {
        this.context.touchListener.onMouseUp.call(this.context, item, this.context.dataAdapter.getItemAt(parseInt(this.getAttribute("__didx")))); }
    item.onblur = function () { this.context.focusListener.onBlur.call(this.context, item, this.context.dataAdapter.getItemAt(parseInt(this.getAttribute("__didx")))); }
}
JUI.Repeater.prototype.addItemAt = function (item, idx) {


}
JUI.Repeater.prototype.fillPattern = function (html, dataIdx) {

    var matchArr = html.match(JUI.Repeater.Utils.regexp);

    for (var j = 0; j < matchArr.length; j++) {

        var dataField = matchArr[j].substring(1, matchArr[j].length - 1);
        if (dataField.length > 0)
            html = html.replace(matchArr[j], this.dataAdapter.getDataValue(dataIdx, dataField));
        else
            html = html.replace(matchArr[j], this.dataAdapter.getItemAt(dataIdx).toString());
    }

    return html;
}